package com.kunbo.cn.app.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.app.dao.NoticeOfArrivalDao;
import com.kunbo.cn.app.entity.NoticeOfArrival;
import com.kunbo.cn.app.service.NoticeOfArrivalService;
import com.kunbo.cn.enumeration.core.logistics.CarState;
import com.kunbo.cn.enumeration.core.logistics.DriverState;

@Service
public class NoticeOfArrivalImpl implements NoticeOfArrivalService{
	
	
	@Autowired
	private NoticeOfArrivalDao dao;
	
	@Autowired
	private JdbcTemplate jdbc;
	


	@Override
	public NoticeOfArrival selArrival(Integer id) {
		// TODO Auto-generated method stub
		NoticeOfArrival arrival = dao.findOne(id);
		String code = arrival.getWaybill_code(); //运单号
		String sql = "select purchase_waybill_code from business_purchase_waybill where id = " + Integer.parseInt(code);
		List<Map<String,Object>> list = jdbc.queryForList(sql);
		for (Map<String, Object> map : list) {
			arrival.setWaybill_code(map.get("purchase_waybill_code").toString());
		}
		String receiving_unit = arrival.getReceiving_unit(); //收货单位
		sql = "select name  from  business_customer_manage where id = " + Integer.parseInt(receiving_unit);
		list = jdbc.queryForList(sql);
		for (Map<String, Object> map : list) {
			arrival.setReceiving_unit(map.get("name").toString());
		}
		String transport_unit = arrival.getTransport_unit();//运输单位
		sql = "select name_of_transport_unit  from  transport_unit_management where id = " + Integer.parseInt(receiving_unit);
		list = jdbc.queryForList(sql);
		for (Map<String, Object> map : list) {
			arrival.setTransport_unit(map.get("name_of_transport_unit").toString());
		}
		String car_number  =  arrival.getCar_number();//车牌
		sql = "select license_plate_number  from  vehicle_management where id = " + Integer.parseInt(car_number);
		list = jdbc.queryForList(sql);
		for (Map<String, Object> map : list) {
			arrival.setCar_number(map.get("license_plate_number").toString());
		}
		String driver_name = arrival.getDriver_name();//司机
		sql = "select name from  driver_management where id = " + Integer.parseInt(car_number);
		list = jdbc.queryForList(sql);
		for (Map<String, Object> map : list) {
			arrival.setDriver_name(map.get("name").toString());
		}
		
		return arrival;
	}

	@Override
	public List<Map<String, Object>> selAllArrival(String notificationNumber,Integer page,Integer pageSize,Integer wayBillUnit) {
		// TODO Auto-generated method stub
		String unit = "";
		String unitSql = "select name_of_transport_unit from transport_unit_management where id = " + wayBillUnit;
		if(jdbc.queryForList(unitSql) != null && jdbc.queryForList(unitSql).size()>0) {
		unit = jdbc.queryForList(unitSql).get(0).get("name_of_transport_unit").toString();
		}
		String sql = "select * from business_notice_of_arrival where 1 = 1";
		if(notificationNumber != null && !notificationNumber.equals("")) {
			sql += " and notification_code like '%" + notificationNumber + "%'";
		}
		sql += " and transport_unit = '" + unit + "'";
		sql += " group by id desc  limit " + (page * pageSize) + " , " + pageSize;
		return jdbc.queryForList(sql);
	}

	@Override
	public Boolean saveArrival(String notification_type, String notification_code, String waybill_code,
			String order_code, String supplier, String material_name, String receiving_unit, String transport_unit,
			String car_number, String frid, String driver_name, String id_number, String note,float accruals,float vehicle_authenticated_load,String uuid,String create_user_id,String volume_to_be_transported,String driver_phone) {
		// TODO Auto-generated method stub
		NoticeOfArrival arrival = new NoticeOfArrival();
		arrival.setNotification_type(notification_type);
		arrival.setNotification_code(notification_code);
		arrival.setWaybill_code(waybill_code);
		arrival.setOrder_code(order_code);
		arrival.setSupplier(supplier);
		arrival.setMaterial_name(material_name);
		arrival.setReceiving_unit(receiving_unit);
		arrival.setTransport_unit(transport_unit);
		arrival.setCar_number(car_number);
		arrival.setFrid(frid);
		arrival.setDriver_name(driver_name);
		arrival.setCard_id(id_number);
		arrival.setNote(note);
		arrival.setAccruals(accruals);
		arrival.setCreate_user_id(create_user_id);
		arrival.setUuid(uuid);
		arrival.setNotice_state("7");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		arrival.setCreate_time(format.format(new Date()));
		arrival.setVehicle_authenticated_load(vehicle_authenticated_load);
		arrival.setVolume_to_be_transported(volume_to_be_transported);
		arrival.setCheck_state("0");
		arrival = dao.save(arrival);
		if(arrival != null) {
			int carUpdate = jdbc.update("update vehicle_management set car_state = '" + CarState.getName(2) + "' where license_plate_number = '" + car_number + "'");
			int driverUpdate = jdbc.update("update driver_management set driver_state = '" +DriverState.getName(2)+ "' where idcrad = '" + id_number +"'");
			if(carUpdate > 0 && driverUpdate > 0 ) {
				return true;
			}
		}else {
			return false;
		}
		return false;
	}

	@Override
	public Boolean updArrival(Integer id, String notification_type, String notification_code, String waybill_code,
			String order_code, String supplier, String material_name, String receiving_unit, String transport_unit,
			String car_number, String frid, String driver_name, String id_number, String note,float accruals,float vehicle_authenticated_load,Double old_net_weight) {
		// TODO Auto-generated method stub
		NoticeOfArrival arrival = dao.findOne(id);
		arrival.setNotification_type(notification_type);
		arrival.setNotification_code(notification_code);
		arrival.setWaybill_code(waybill_code);
		arrival.setOrder_code(order_code);
		arrival.setSupplier(supplier);
		arrival.setMaterial_name(material_name);
		arrival.setReceiving_unit(receiving_unit);
		arrival.setTransport_unit(transport_unit);
		arrival.setCar_number(car_number);
		arrival.setFrid(frid);
		arrival.setDriver_name(driver_name);
		arrival.setCard_id(id_number);
		arrival.setNote(note);
		arrival.setAccruals(accruals);
		arrival.setVehicle_authenticated_load(vehicle_authenticated_load);
		arrival.setOld_net_weight(old_net_weight);
		arrival = dao.save(arrival);
		if(arrival != null)
			return true;
		else
			return false;
	}

	@Override
	public List<Map<String, Object>> selOrderArrival(String supplier, Integer page, Integer pageSize) {
		// TODO Auto-generated method stub
		String sql = "select * from business_notice_of_arrival where 1 = 1";
		if(supplier != null  && !supplier.isEmpty()) {
			String suppliers = "select name from business_supplier_manage where id = " + Integer.parseInt(supplier);
			Map<String,Object> map = jdbc.queryForMap(suppliers);
			if(map.size() > 0 ) {
				sql += " and transport_unit  = '" + map.get("name").toString() + "'";
			}
			
		}
		sql += " order by id desc limit " + (page * pageSize) + "," + pageSize;
		System.out.println(sql+"&&&&&&&&&&&&&&66666&&&&&&&&&&&&&");
		return jdbc.queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> selOrderArrivalNotice(String supplier, Integer page, Integer pageSize) {
		// TODO Auto-generated method stub
		String sql = "select * from business_notice_of_arrival where 1 = 1";
		if(supplier != null  && !supplier.isEmpty()) {
			String suppliers = "select name from business_supplier_manage where id = " + Integer.parseInt(supplier);
			Map<String,Object> map = jdbc.queryForMap(suppliers);
			if(map.size() > 0 ) {
				sql += " and supplier  = '" + map.get("name").toString() + "'";
			}
			
		}
		sql += " order by id desc limit " + (page * pageSize) + "," + pageSize;
		System.out.println(sql+"&&&&&&&&&&&&&&66666&&&&&&&&&&&&&");
		return jdbc.queryForList(sql);
	}

}
